package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.VmTypeMapper;
import com.heima.pojo.entity.*;
import com.heima.pojo.vo.PageVo;
import com.heima.service.VmTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VmTypeServiceImpl implements VmTypeService {
    @Autowired
    private VmTypeMapper vmTypeMapper;

    /**
     * 设备搜索 分页条件查询
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public PageVo getTypePage(Integer pageIndex, Integer pageSize, String name) {
        //先获取非集合的几个数据并进行封装
        PageVo<TbVmType> vmTypePageVo = new PageVo<>();
        //进行分页和查询 类型表
        PageHelper.startPage(pageIndex,pageSize);
        LambdaQueryWrapper<TbVmType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(name!=null,TbVmType::getName,name);
        List<TbVmType> tbVmTypes= vmTypeMapper.selectList(queryWrapper);
        //得到的type集合放入返回集合中
        vmTypePageVo.setCurrentPageRecords(tbVmTypes);
        //将查询结果强转为page 并封装要返回的数据
        Page<TbVmType> page = (Page<TbVmType>) tbVmTypes;
        vmTypePageVo.setPageIndex(pageIndex);
        vmTypePageVo.setPageSize(pageSize);
        vmTypePageVo.setTotalCount(page.getTotal());
        vmTypePageVo.setTotalPage(page.getPages());
        return vmTypePageVo;
    }

    /**
     * 新增设备类型
     * @param type
     * @return
     */
    @Override
    public Boolean save(TbVmType type) {
        int insert = vmTypeMapper.insert(type);
        if (insert==1){
            return true;
        }
        return false;
    }

    /**
     * 修改设备
     * @param typeId
     * @param type
     * @return
     */
    @Override
    public Boolean update(Integer typeId, TbVmType type) {
        LambdaQueryWrapper<TbVmType> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(TbVmType::getTypeId,typeId);
        int i = vmTypeMapper.update(type,queryWrapper);
        if (i==1){
            return true;
        }
        return false;
    }

    /**
     * 删除设备
     * @param typeId
     * @return
     */
    @Override
    public Boolean delete(Integer typeId) {
        LambdaQueryWrapper<TbVmType> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(TbVmType::getTypeId,typeId);
        int i = vmTypeMapper.delete(queryWrapper);
        if (i==1){
            return true;
        }
        return false;
    }
}
